<?php 
//include 'common.inc.php';
 //echo date('His');
$configargs = array(  
    "config" => "E:/APMServ5.2.6/PHP/extras/openssl/openssl.cnf"  
);  


  
//$res = openssl_pkey_new();   
//$res = openssl_pkey_new($configargs);  
  
// Get private key   
//openssl_pkey_export($res, $privkey,null,$configargs);  
  
// // Get public key   
//$pubkey =   openssl_pkey_get_details($res);  
//$pubkey =   $pubkey["key"];  
$privkey= <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDhfouGSqiYnD+LxhTnOZc1YbF42dx4sutwd5iAMXoLcCIXKCDN
4NTgdfwMpb6eVjv6EzQl/xXwsBScPnmUWtPrE+BLQxjrmk8OJXp+HFPoiPIc37Sg
wnS+8ltRqnI8us0Pa3F0X5b7k+cawScuS+Rnf48WdOp8CMAuqYsAo33l5QIDAQAB
AoGAJKvRwPsvQ/JVG0QRbjEdeB71H7Q+eJqQU2//qwlg0a9gu+dYvmKL/Bzz6pKh
11oq0taAqpU7WjMohuhaGVhUEh6kMUKSjCcd8P/aaV5FvR21bot4wY5GBukIQYzN
Tb6NB0ZlqRTAKyR22qESRGgU87K5PPJHVjH1rzEriYbOsFkCQQD7qcS1PDo4NtqO
H1GB9xW1C4SauKN3eNQ8TgC7EifR4QpVk1+MF1RGM+zawCAO+mHnuwUP84G/7Qs6
0AYbmTeXAkEA5WFUq9fS0496yj5emF3MjELrc40K6Uhd1dNqRjWaICX1iNm46ulj
122LYsELlmDT4WbVyiDO7BB04t8KCgad4wJAez7Nza/cFzGN4PIneY1mvQ4R96+a
pmSvE4VjyDmA9MDPzc7piPLX27WMNijGym8GSXUGaqRnE3Xj/P5pL1TeqwJBALYY
VXpyRHkVD1qctsXouOike3xo/L+DZpLIWjbQeMGKwOhaR3WdwFgWK7BDJmrEBZCL
7a6f6wT2b4RnofWhmRcCQHO1HPmz7eD3b1+TDHzGXQU9D4mWJMwO3utORSxnuRSz
mdrFyC8WK+4H52Sq4ydWTQtwThlIvDL7uA1P9/8tbsQ=
-----END RSA PRIVATE KEY-----
EOD;
//var_dump($privkey,$pubkey);  

// 现在$pri为私钥, $pub为对应的公钥. 将$pri保密, $pub可以对外公开

// 现在用私钥$pri对字串'hello'进行签名
 //openssl_sign('hello', $sig, openssl_pkey_get_private($privkey),OPENSSL_ALGO_MD5);
//echo $sig;
//var_dump(base64_encode($sig));
// 现在$sig就是字串'hello'的数字签名了, 可以证明'hello'是经过你验证的数据

// 现在将'hello'和$sig发送给别人, 别人收到'hello'和$sig后, 为了证明
// 'hello'就是你本人发给他的原始数据, 没有被篡改, 可以用你公开的公钥$pub
// 来验证, 步骤如下

// 别人收到了'hello'和$sig, 且知道你的公钥是$pub
// if (openssl_verify('hello', $sig, openssl_pkey_get_public($privkey),OPENSSL_ALGO_MD5))
// 	echo '验证通过';
// else
// 	echo '验证失败, 消息被篡改';



 

//test
$input = '<?xml version="1.0" encoding="gb2312"?>
<message>
	<head>
		<field pin="false" name="version"><![CDATA[100]]></field>
		<field pin="false" name="type"><![CDATA[0200]]></field>
		<field pin="false" name="chanl_no"><![CDATA[30]]></field>
		<field pin="false" name="chanl_sub_no"><![CDATA[3001]]></field>
		<field pin="false" name="chanl_date"><![CDATA[20111223]]></field>
		<field pin="false" name="chanl_time"><![CDATA[160855]]></field>
		<field pin="false" name="ectip_date"><![CDATA[20111223]]></field>
		<field pin="false" name="chanl_flow_no"><![CDATA[]]></field>
		<field pin="false" name="ectip_flow_no"><![CDATA[2012010230378982602]]></field>
		<field pin="false" name="chanl_trad_no"><![CDATA[3FC019]]></field>
		<field pin="false" name="term_inf"><![CDATA[64.0.98.97]]></field>
		<field pin="false" name="resp_code"><![CDATA[]]></field>
		<field pin="false" name="resp_msg"><![CDATA[]]></field>
		<field pin="false" name="page">0</field>
		<field pin="false" name="maxrow"><![CDATA[]]></field>
		<field pin="false" name="locstr"><![CDATA[]]></field>
		<field pin="false" name="tot_rec"><![CDATA[]]></field>
	</head>
	<body>
		<field pin="false" name="MCH_NO"><![CDATA[1100000048]]></field>
		<field pin="false" name="BUYER_SIT_NO"><![CDATA[100014]]></field>
		<field pin="false" name="SELLER_SIT_NO"><![CDATA[100016]]></field>
		<field pin="false" name="CTRT_NO"><![CDATA[M2012042515263538]]></field>
		<field pin="false" name="PAY_PRD_NO"><![CDATA[01]]></field>
		<field pin="false" name="PAY_STS"><![CDATA[1]]></field>
		<field pin="false" name="TX_AMT">1.00</field>
		<field pin="false" name="BUYER_SVC_AMT">0.00</field>
		<field pin="false" name="SELLER_SVC_AMT">0.00</field>
	</body>
</message>';
// $miwen='H4yTR2pLrAV0V+yOjJnoTy+6Tu0NbsCwP4x+bqtCgp28Ln8ZpB2d/Nq0+uGORaRhPWN+yr8Ss8rUMrcxtR+0KSB1CmjpnXFbOQSORVjF8mfG2L+/OqTy1x8je5/buHrR7PIxH9lVa1VSj9Q+QFCRXqw6M1kgsMtRVa9aFYQrE6OCm74U7eUAQHxuye2WU0w5AWwijcvoftu2kuQmNM9WCG9WCdNijhhUtoxZlIW5MrL4fEwCYBj5N0o+Le0NlBBC8BvBP+CHz3DyRzKgh5Fu/YvBJfX5J//Yd96Coqjp7gEgnE2vV4Ax4pXxl/KWeJSZJBOgxggd//FHRGR9lQwE4ztkkhcB2hy29bRpTYdQ/xKLwSX1+Sf/2HfegqKo6e4Bg+apaLudeaesOjNZILDLUVWvWhWEKxOjgpu+FO3lAEB8bsntllNMOT3RsyICA6ccbi5oHlfvDc9lK0Ok9XT7t36lzXlmf1ew1DK3MbUftCkgdQpo6Z1xWzkEjkVYxfJneMeQykvkYnBSCSkd0nYyEaR80ZLe6wyI8HyIIThO7iCsOjNZILDLUVWvWhWEKxOjgpu+FO3lAEB8bsntllNMObwt7N8gZLDXbi5oHlfvDc/FyOTwXK/i7oNBC+l+TI/ZfqXNeWZ/V7DUMrcxtR+0KSB1CmjpnXFbOQSORVjF8md4x5DKS+RicPd2q/+Dqgw3/ZjI0IYKPGrB07MuMa5NT5fi7qLSqB2BaAXIvEQwWc5nR5OPU4iqJtWxTJ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3TcrShK5E1oK1R68Ip/DCRY6iDUWKp8JRja6QIw0xgzem6KAhMf426Cu68IJxNr1eAMeLGN1hpmnNYwFWvWhWEKxOjgpu+FO3lAEB8bsntllNMOVZZrCnp0gOkaXOFkK+kRWRKJVn0jQbkxmj9bPcu7YaPl+LuotKoHYESDbReDncJ5Vq2b4QRuJ9Gsl4zQqS27BxhPzsdp1TCNuXuptGIKd+xpHzRkt7rDIgMtfsk4e43GXE24Uhis91LDQLyA8w9vuCX4u6i0qgdgRINtF4OdwnlWrZvhBG4n0ayXjNCpLbsHGhly9aX3Fow4UKa/se2bYzB07MuMa5NT5fi7qLSqB2BxmGBc5g1+MGuIBdKYut/UED/2u7Nh5KlriAXSmLrf1D1wWl+ii+M9PCcDND6IJlfeh9tWDeruZw=';
/*function encrypt($data, $key, $use3des = true) {
    $cipher =  MCRYPT_TRIPLEDES ;
    $modes = MCRYPT_MODE_ECB;
    $key = base64_decode($key);
    # Add PKCS7 padding.
    $block = mcrypt_get_block_size($cipher, $modes);
    $pad = $block - (strlen($data) % $block);
    $data .= str_repeat(chr($pad), $pad);

    $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, $modes), MCRYPT_RAND);
    $encrypted = @mcrypt_encrypt($cipher, $key, $data, $modes, $iv);
    
    return $encrypted;
}
function decrypt($data, $key, $use3des = true) {
	$cipher = $use3des ? MCRYPT_TRIPLEDES : MCRYPT_DES;
	$modes = MCRYPT_MODE_ECB;
	$key = base64_decode($key);
	$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( $cipher, $modes ), MCRYPT_RAND );
	$data = @mcrypt_decrypt ( $cipher, $key, $data, $modes, $iv ); // Strip
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   // padding
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   // out.
	$block = mcrypt_get_block_size ( $cipher, $modes );
	$pad = ord ( $data [($len = strlen ( $data )) - 1] );
	$decrypted = substr ( $data, 0, strlen ( $data ) - $pad );
	return $decrypted;
}
  $key='sLYgO4YNomGt44aUH7+1YrC2IDuGDaJh';*/

// //echo "plainText:" . base64_encode('4')."<br/>";

require   'include/ccb.class.php';
require   'include/ccb.func.php';
$ccb = new ccb();
//echo $ccb->deskey;
$xml=base64_encode($ccb->encrypt($input));
$rst = $ccb->post('http://127.0.0.1/api/ccb.php', 'xml='.urlencode($xml).'&signature=12121');
echo '//'.$rst;
if($rst != false){
	$resMsg =  $ccb->decrypt(substr($rst, 138,strlen($rst)-138));

	//echo $resMsg;
	if(mb_check_encoding($resMsg,'utf-8')){
		$resMsg = mb_convert_encoding($resMsg, 'utf-8','auto');//
	}else{
		$resMsg =	iconv("gb2312", "utf-8", $resMsg);
	}
	$resMsg=str_replace('gb2312', 'utf-8', $resMsg);
	$resMsg= $ccb->XMLtoArray($resMsg);
}else{
	$resMsg['HEAD']['resp_code'] = '';
	$resMsg['HEAD']['resp_msg']='请求有误';
	$resMsg['BODY'] ['FIELD'] = '';
	$resMsg['BODY'] ['FIELD-LIST'] = '';
}
 var_dump($resMsg);
  //echo "Encode:".base64_encode($ccb->encrypt($input))."<br/>";
 
//  echo "Miwene:".$miwen."<br/>";
// echo "Decode:". decrypt(base64_decode(encrypt($input,$key)),$key).'</br>';
// echo "Decode:".decrypt(base64_decode($miwen),$key).'';
// //echo TripleDES::Decrypt($key, base64_decode($input));
?>